package cn.edu.scau.dao;

import cn.edu.scau.entity.RecoveryTrack;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 数据访问层 RecoveryTrackSqlProvider： 是接口RecoveryTrackMapper的补充，专门编写复杂的SQL语句
 * @author LLLegend
 *
 */
public class RecoveryTrackSqlProvider {

    public String insertSelective(RecoveryTrack record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("recoverytrack");
        
        if (record.getRecoveryTrackDate() != null) {
            sql.VALUES("recoveryTrackDate", "#{recoveryTrackDate,jdbcType=DATE}");
        }
        
        if (record.getRecoveryRegisterID() != null) {
            sql.VALUES("recoveryRegisterID", "#{recoveryRegisterID,jdbcType=INTEGER}");
        }
        
        if (record.getRecoveryTrackRecording() != null) {
            sql.VALUES("recoveryTrackRecording", "#{recoveryTrackRecording,jdbcType=VARCHAR}");
        }
        
        if (record.getRecoveryTrackCustomerStatus() != null) {
            sql.VALUES("recoveryTrackCustomerStatus", "#{recoveryTrackCustomerStatus,jdbcType=VARCHAR}");
        }
        
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(RecoveryTrack record) {
        SQL sql = new SQL();
        sql.UPDATE("recoverytrack");
        
        if (record.getRecoveryTrackRecording() != null) {
            sql.SET("recoveryTrackRecording = #{recoveryTrackRecording,jdbcType=VARCHAR}");
        }
        
        if (record.getRecoveryTrackCustomerStatus() != null) {
            sql.SET("recoveryTrackCustomerStatus = #{recoveryTrackCustomerStatus,jdbcType=VARCHAR}");
        }
        
        sql.WHERE("recoveryTrackDate = #{recoveryTrackDate,jdbcType=DATE}");
        sql.WHERE("recoveryRegisterID = #{recoveryRegisterID,jdbcType=INTEGER}");
        
        return sql.toString();
    }
    
    public String find(Date recoveryTrackDate,Integer recoveryRegisterID,String customerName){
        SQL sql = new SQL();
        sql.SELECT("recoveryTrackDate, recoveryRegisterID, customerName, recoveryTrackRecording, recoveryTrackCustomerStatus");
        sql.FROM("recoverytrack natural join recoveryregister natural join customer");
        if(recoveryTrackDate!=null){
        	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            sql.WHERE(" recoveryTrackDate = \""+sdf.format(recoveryTrackDate)+"\"");
            System.out.println(sdf.format(recoveryTrackDate));
        }
        if(recoveryRegisterID!=null){
            sql.WHERE(" recoveryRegisterID like concat('%',#{recoveryRegisterID},'%')");
        }
        if(customerName!=null){
            sql.WHERE(" customerName like concat('%',#{customerName},'%')");
        }
        System.out.println(sql.toString());
        return sql.toString();
        
    }
    
    public String findWithNurseID(Date recoveryTrackDate,Integer recoveryRegisterID,String customerName,Integer nurseID){
        SQL sql = new SQL();
        sql.SELECT("recoveryTrackDate, recoveryRegisterID, customerName, recoveryTrackRecording, recoveryTrackCustomerStatus");
        sql.FROM("recoverytrack natural join recoveryregister natural join customer natural join nurse");
        if(recoveryTrackDate!=null){
        	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            sql.WHERE(" recoveryTrackDate = \""+sdf.format(recoveryTrackDate)+"\"");
            System.out.println(sdf.format(recoveryTrackDate));
        }
        if(recoveryRegisterID!=null){
            sql.WHERE(" recoveryRegisterID like concat('%',#{recoveryRegisterID},'%')");
        }
        if(customerName!=null){
            sql.WHERE(" customerName like concat('%',#{customerName},'%')");
        }
        sql.WHERE(" nurseID = #{nurseID}");
        System.out.println(sql.toString());
        return sql.toString();
        
    }
}